package com.example.flink.service;

import com.example.flink.LogEvent;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class KafkaProducerService {
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Async
    public void sendLog(LogEvent logEvent) {
        try {
            String json = objectMapper.writeValueAsString(logEvent);
            kafkaTemplate.send("user_log", json);
        } catch (Exception e) {
            throw new RuntimeException("发送日志到Kafka失败", e);
        }
    }
} 